package com.mxgraph.examples.swing.editor;

import java.util.Map;

import javax.swing.JMenu;
import javax.swing.JPopupMenu;
import javax.swing.TransferHandler;

import utils.Parseador;

import com.mxgraph.examples.swing.editor.EditorActions.HistoryAction;
import com.mxgraph.model.mxCell;
import com.mxgraph.swing.util.mxGraphActions;
import com.mxgraph.util.mxResources;

import constants.DiagramaConstants;

public class EditorPopupMenu extends JPopupMenu
{

	/**
	 * 
	 */
	private static final long serialVersionUID = -3132749140550242191L;
	private String typeObject;
	private String name;
	private String reference;
	

	public EditorPopupMenu(BasicGraphEditor editor)
	{
		name = "";
		typeObject = "";
		boolean selected = !editor.getGraphComponent().getGraph()
				.isSelectionEmpty();
		
		if (selected){	
			name = editor.getGraphComponent().getGraph().getLabel(editor.getGraphComponent().getGraph().getSelectionCell());
			double size = editor.getGraphComponent().getGraph().getCellGeometry(editor.getGraphComponent().getGraph().getSelectionCell()).getWidth();
			Map<String,Object> styleObject = editor.getGraphComponent().getGraph().getCellStyle(editor.getGraphComponent().getGraph().getSelectionCell());

			mxCell selectedObject = (mxCell) editor.getGraphComponent().getGraph().getSelectionCell();
			
			
			//
//			Por ahora no se edita el atributo, se borra y se vuelve a crear.
//			if (size == 20){
//				typeObject = DiagramaConstants.ATRIBUTO;
//				reference = editor.getGraphComponent().getGraph().getCellReference(editor.getGraphComponent().getGraph().getSelectionCell());
//			}
//			else {
				if (styleObject.containsValue("rhombusPerimeter")){
					typeObject = DiagramaConstants.RELACION;
				}
				else if (styleObject.containsValue("rectanglePerimeter")){
					typeObject = DiagramaConstants.ENTIDAD;
				}
				//si el objeto seleccionado es una jerarquia
				else if (selectedObject.isEdge() && (selectedObject.getValue().toString().equals("(T,E)") || selectedObject.getValue().toString().equals("(P,E)")
						|| selectedObject.getValue().toString().equals("(T,S)") || selectedObject.getValue().toString().equals("(P,S)"))){
					
					typeObject = DiagramaConstants.JERARQUIA;
					name = selectedObject.getStyle();
					
				}
				else {
				
				    if (styleObject.containsValue("ellipsePerimeter")){
				    	reference = editor.getGraphComponent().getGraph().getCellReference(editor.getGraphComponent().getGraph().getSelectionCell());
				    	String typeElement = Parseador.obtenerTipoDeAtributoReferenciaDelNombreDelAtributo(reference);
				    	if ( typeElement.equals(DiagramaConstants.TIPO_ATRIBUTO_COMPUESTO)){
				    		typeObject = DiagramaConstants.ATRIBUTO_COMPUESTO;
				    	}
				    	else{
				    		typeObject = DiagramaConstants.ATRIBUTO;
				    	}
				    }
				
					
				}
			
				
			//}
		}
		//Agregamos las acciones.
		add(
				editor.bind(mxResources.get("edit"), mxGraphActions
						.getEditAction(name,typeObject,reference))).setEnabled(selected);
	
//		addSeparator();
//
//		add(editor.bind(mxResources.get("selectVertices"), mxGraphActions
//				.getSelectVerticesAction()));
//		add(editor.bind(mxResources.get("selectEdges"), mxGraphActions
//				.getSelectEdgesAction()));
//
//		add(editor.bind(mxResources.get("selectAll"), mxGraphActions
//				.getSelectAllAction()));
		
		addSeparator();
		
		add(editor.bind(mxResources.get("undo"), new HistoryAction(true),
				"/com/mxgraph/examples/swing/images/undo.gif"));

//		add(
//				editor.bind(mxResources.get("cut"), TransferHandler
//						.getCutAction(),
//						"/com/mxgraph/examples/swing/images/cut.gif"))
//				.setEnabled(selected);
//		add(
//				editor.bind(mxResources.get("copy"), TransferHandler
//						.getCopyAction(),
//						"/com/mxgraph/examples/swing/images/copy.gif"))
//				.setEnabled(selected);
//		add(editor.bind(mxResources.get("paste"), TransferHandler
//				.getPasteAction(),
//				"/com/mxgraph/examples/swing/images/paste.gif"));

		addSeparator();

		add(
				editor.bind(mxResources.get("delete"),
						mxGraphActions.getDeleteAction(name,typeObject,reference),
						"/com/mxgraph/examples/swing/images/delete.gif"))
				.setEnabled(selected);

	}

}
